package cn.iocoder.yudao.module.product.controller.app.category;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryGoodsRespVO;
import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryRespVO;
import cn.iocoder.yudao.module.product.controller.app.category.vo.AppCategoryWithGoodsRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 商品分类")
@RestController
@RequestMapping("/product/category")
@Validated
public class AppCategoryController {

    @Resource
    private ProductCategoryService categoryService;

    @GetMapping("/list")
    @Operation(summary = "获得商品分类列表")
    public CommonResult<List<AppCategoryRespVO>> getProductCategoryList() {
        List<ProductCategoryDO> list = categoryService.getEnableCategoryList();
        list.sort(Comparator.comparing(ProductCategoryDO::getSort));
        return success(BeanUtils.toBean(list, AppCategoryRespVO.class));
    }

    @GetMapping("/listWithGoods")
    public CommonResult<List<AppCategoryWithGoodsRespVO>> getProductCategoryBanner(){
        //获取有效分类
        List<ProductCategoryDO> list = categoryService.getEnableCategoryList();
        list.sort(Comparator.comparing(ProductCategoryDO::getSort));
        return success(buildDeptTreeSelect(BeanUtils.toBean(list, AppCategoryWithGoodsRespVO.class)));
    }

    public List<AppCategoryWithGoodsRespVO> buildDeptTreeSelect(List<AppCategoryWithGoodsRespVO> depts)
    {
        return buildDeptTree(depts);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    public List<AppCategoryWithGoodsRespVO> buildDeptTree(List<AppCategoryWithGoodsRespVO> depts)
    {
        List<AppCategoryWithGoodsRespVO> returnList = new ArrayList<AppCategoryWithGoodsRespVO>();
        List<Long> tempList = new ArrayList<Long>();
        for (AppCategoryWithGoodsRespVO dept : depts)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<AppCategoryWithGoodsRespVO> iterator = depts.iterator(); iterator.hasNext();)
        {
            AppCategoryWithGoodsRespVO dept = (AppCategoryWithGoodsRespVO) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<AppCategoryWithGoodsRespVO> list, AppCategoryWithGoodsRespVO t)
    {
        // 得到子节点列表
        List<AppCategoryWithGoodsRespVO> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AppCategoryWithGoodsRespVO tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AppCategoryWithGoodsRespVO> list, AppCategoryWithGoodsRespVO t)
    {
        return !getChildList(list, t).isEmpty();
    }

    /**
     * 得到子节点列表
     */
    private List<AppCategoryWithGoodsRespVO> getChildList(List<AppCategoryWithGoodsRespVO> list, AppCategoryWithGoodsRespVO t)
    {
        List<AppCategoryWithGoodsRespVO> tlist = new ArrayList<AppCategoryWithGoodsRespVO>();
        Iterator<AppCategoryWithGoodsRespVO> it = list.iterator();
        while (it.hasNext())
        {
            AppCategoryWithGoodsRespVO n = (AppCategoryWithGoodsRespVO) it.next();
            if (n.getParentId()!=null && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }


    @GetMapping("/list-by-ids")
    @Operation(summary = "获得商品分类列表，指定编号")
    @Parameter(name = "ids", description = "商品分类编号数组", required = true)
    public CommonResult<List<AppCategoryRespVO>> getProductCategoryList(@RequestParam("ids") List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return success(Collections.emptyList());
        }
        List<ProductCategoryDO> list = categoryService.getEnableCategoryList(ids);
        list.sort(Comparator.comparing(ProductCategoryDO::getSort));
        return success(BeanUtils.toBean(list, AppCategoryRespVO.class));
    }

}
